using System;
using System.Collections.Generic;
using System.IO;
using Avalonia.Platform;
using Newtonsoft.Json;

namespace LuckyCoding.Models;

[Serializable]
public class SettingModel
{
    public EditSetting? Edit { get; set; }
    public List<IconSetting> Icons { get; set; } = new ();
    public List<ManagerSetting>? Managers { get; set; } = new ();
}

public class EditSetting
{
    public string? Theme { get; set; }
    public bool IsShowTabs { get; set; }
    public bool IsShowSpaces { get; set; }
    public bool IsShowEndOfLine { get; set; }
    public string? Font { get; set; }
}

public class IconSetting
{
    public string? Name { get; set; }
    public string? IconPath { get; set; }
}

public class ManagerSetting
{
    public string? Name { get; set; }
    public string? ImagePath { get; set; }
}

public static class SettingStatic
{
    public static void Save(this SettingModel model)
    {
        var a = AssetLoader.Open(new Uri("avares://LuckyCoding/Assets/setting.json"));
        var s = JsonConvert.SerializeObject(model);
        var writer = new StreamWriter(a);
        writer.Write(s);
        writer.Dispose();
        a.Dispose();
    }

    public static SettingModel? Open()
    {
        using var a = AssetLoader.Open(new Uri("avares://LuckyCoding/Assets/setting.json"));
        using var reader = new StreamReader(a);
        var s = reader.ReadToEnd();
        return JsonConvert.DeserializeObject<SettingModel>(s);
    }
}